
# delim ;  
clear all;
set matsize 11000 ;
eststo clear ;
capture program drop add_lab_FE;
capture log close ; 
log using "../logs/universe_regressions.log", text replace;


*-------------------------****** OUTPUT SETTINGS *****---------------------;


*Some settings that are common for the set of plots on this dofile; 
local pdf_plot_settings = 	`"scale(*1.5) ymtick(##4) leg(off) xtitle("")"';

*settings for latex tables ;
local tex_settigns  = `" prehead(\begin{table}[!htb]\centering
	\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
	\caption{@title}
	\setlength{\tabcolsep}{3pt}
	\begin{tabular}{l*{@span}{c}}
	\hline\hline)
	postfoot(\hline\hline
	\multicolumn{@span}{l}{\footnotesize Standard errors clustered by state in parenthesis.  }\\
	\multicolumn{@span}{l}{\footnotesize @starlegend.   }
	\end{tabular}\end{table})	"'	;


	
local tex_settigns_adj  = `" 
	prehead(\begin{table}[!htb]\centering
	\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
	\begin{adjustbox}{max width=\textwidth,max totalheight=\textheight}
	\caption{@title}
	\begin{tabular}{l*{@span}{c}}
	\hline\hline)
	postfoot(\hline\hline
	\multicolumn{@span}{l}{\footnotesize Standard errors clustered by state in parenthesis.  }\\
	\multicolumn{@span}{l}{\footnotesize @starlegend.   }
	\end{tabular}\end{adjustbox}\end{table})		"'	;
	

*----------------------------------------------------------------------------------;

****************************************;	

*Define price index ;
use "../data/state_year_all_80_15.dta", clear  ;

*---------------------------------------------------;
* - Generate variables and general cleanings ;
*---------------------------------------------------;

do "_setup_expenditure.do";
do "_cleaning_and_new_variables.do";
do "_programs_universe";

tab year periods ;

drop if year<1984 ;
drop if exp_L_IH_SF12a_r==.;
drop if exp_L_IH_SF12a_r==0;
drop if exp_L_IH_SF12a_r<0;


sum app_IH_r exp_L_IH_SF12a_r;
*------------------------------------------------;
* Run the regressions ;
*------------------------------------------------;
*All regressions;

*IV Definition:;
*---------------------------------;
local IV L4FHWA_apport_LW_r;

label var time2 "\( t^2\)";
gen exp_L_IH_SF12a_rXtime=exp_L_IH_SF12a_r*time;
gen exp_L_IH_SF12a_rXtime2=exp_L_IH_SF12a_r*time2;

gen `IV'Xtime =`IV'*time;
gen `IV'_sample=1 if `IV'!=.;

gen time_92=time-8 if year>1991;
tab time_92 year;


gen exp_L_IH_SF12a_rXtime_92=exp_L_IH_SF12a_r*time_92;
local IV L4FHWA_apport_LW_r;
gen `IV'Xtime_92 =`IV'*time_92;


label var time "\(t\) ";
label var `IV'Xtime " \( app_{(t-4)} t \) ";
label var L4FHWA_apport_LW_r "\( app_{(t-4)}\)";

tab year;
label var  exp_L_IH_SF12a_rXtime "\(I^{L}_{st} t \)  ";
label var  exp_L_IH_SF12a_rXtime2 "\(I^{L}_{st} t^2\)  ";

local listcontinous "c.time cont Baseline" ;
local listcontinous2 "c.time_+_c.time2 cont_2 " ;
local listdiscrete  "ib2.periods dum Periods" ;


	eststo ,prefix(filter_): reg Du_lane_miles_IH exp_L_IH_SF12a_r,  cluster(state);
	sum_stats;
	eststo ,prefix(filter_): reg Du_lane_miles_IH exp_L_IH_SF12a_r 	io36.state,   cluster(state);	
	sum_stats;
	eststo ,prefix(filter_): reg Du_lane_miles_IH 	time   exp_L_IH_SF12a_r exp_L_IH_SF12a_rXtime   ,   cluster(state);
	
	eststo ,prefix(filter_): reg Du_lane_miles_IH 	 time time2  exp_L_IH_SF12a_r exp_L_IH_SF12a_rXtime  exp_L_IH_SF12a_rXtime2   ,   cluster(state);
	
	ren time time_old;
	ren exp_L_IH_SF12a_rXtime exp_L_IH_SF12a_rXtime_old;
	ren exp_L_IH_SF12a_rXtime_92 exp_L_IH_SF12a_rXtime;
	ren time_92 time;
	sum_stats;
	
	eststo ,prefix(filter_): reg Du_lane_miles_IH 	time   exp_L_IH_SF12a_r exp_L_IH_SF12a_rXtime   if year>1991 ,   cluster(state);
	sum_stats;
	ren  exp_L_IH_SF12a_rXtime exp_L_IH_SF12a_rXtime_92;
	ren time time_92 ;
	ren time_old  time;
	ren  exp_L_IH_SF12a_rXtime_old exp_L_IH_SF12a_rXtime;
	
	eststo ,prefix(filter_): reg Du_lane_miles_IH 	 time   exp_L_IH_SF12a_r exp_L_IH_SF12a_rXtime  io36.state ,   cluster(state);
	sum_stats;
	
	eststo ,prefix(filter_): reg Du_lane_miles_IH 	 time time2  exp_L_IH_SF12a_r exp_L_IH_SF12a_rXtime  exp_L_IH_SF12a_rXtime2  io36.state ,   cluster(state);
	sum_stats;
	
	ren time time_old;
	ren exp_L_IH_SF12a_rXtime exp_L_IH_SF12a_rXtime_old;
	ren exp_L_IH_SF12a_rXtime_92 exp_L_IH_SF12a_rXtime;
	ren time_92 time;

	eststo ,prefix(filter_): reg Du_lane_miles_IH 	 time   exp_L_IH_SF12a_r exp_L_IH_SF12a_rXtime  io36.state  if year>1991,   cluster(state);
	sum_stats;
		
	ren  exp_L_IH_SF12a_rXtime exp_L_IH_SF12a_rXtime_92;
	ren time time_92 ;
	ren time_old  time;
	ren  exp_L_IH_SF12a_rXtime_old exp_L_IH_SF12a_rXtime;

		

	
	eststo ,prefix(filter_): ivreg2  Du_lane_miles_IH 	 time   (exp_L_IH_SF12a_r exp_L_IH_SF12a_rXtime=`IV' `IV'Xtime),   robust first  ffirst  saverf saverfprefix(rf_3) savefirst  savefprefix(fs_3) cluster(state);
	ren time time_old;
	ren exp_L_IH_SF12a_rXtime exp_L_IH_SF12a_rXtime_old;
	ren exp_L_IH_SF12a_rXtime_92 exp_L_IH_SF12a_rXtime;
	ren time_92 time;
	ren `IV'Xtime `IV'Xtime_old;
	ren `IV'Xtime_92 `IV'Xtime;
	
	eststo ,prefix(filter_): ivreg2  Du_lane_miles_IH 	 time   (exp_L_IH_SF12a_r exp_L_IH_SF12a_rXtime=`IV' `IV'Xtime) if year>1991,   robust first  ffirst  saverf saverfprefix(rf_3) savefirst  savefprefix(fs_3) cluster(state);
	sum_stats;
	
	ren  exp_L_IH_SF12a_rXtime exp_L_IH_SF12a_rXtime_92;
	ren time time_92 ;
	ren time_old  time;
	ren  exp_L_IH_SF12a_rXtime_old exp_L_IH_SF12a_rXtime;
	ren  `IV'Xtime `IV'Xtime_92;
	ren `IV'Xtime_old `IV'Xtime ;

	
	*change tex ->  csv if you want to see on excel;
	esttab filter_1 filter_2 filter_3  filter_5  using "${output}/tables/Table3_universe_regressions.tex", replace
	
	    star(+ 0.10 * 0.05 ** 0.01 *** 0.001) 
		b(%9.4fc) se(%9.4fc)  sfmt(%9.3fc)
		title("Construction expenditure and new lane miles\label{tab:table4}")
		indicate( "State FE=*state" )
		nomtitles  noomitted   nobaselevels 
		scalars(  "mean_Du_lane_miles_IH \(\Delta_t  \: L_{st}\)"
				  "mean_exp_L_IH_SF12a_r \(I^{L}_{st}\)") 
		substitute(\_ _
					"State FE" "\cline{2-5}State FE")
		se  par  r2 label nogaps drop(_cons)  compress
		obslast stats(N widstat ,  fmt(%9.0fc %9.2fc) labels(`"N"' `"\(F\)"'))
			/* Only when exporting to tex*/
		`tex_settigns'
		;
				

		
	
	*change tex ->  csv if you want to see on excel;
	esttab filter_9  using "${output}/tables/TableB2_universe_regressions.tex", replace
	
	    star(+ 0.10 * 0.05 ** 0.01 *** 0.001) 
		b(%9.4fc) se(%9.4fc)  sfmt(%9.3fc)
		title("Construction expenditure and new lane miles, TSLS estimate\label{tab:tableB2}")
		
		nomtitles  noomitted   nobaselevels 
		scalars(  "mean_Du_lane_miles_IH \(\Delta_t  \: L_{st}\)"
				  "mean_exp_L_IH_SF12a_r \(I^{\ell}_{st}\)") 
		substitute(\_ _
					"State FE" "\cline{2-6}State FE")
		se  par  r2 label nogaps drop(_cons)  compress
		obslast stats(N widstat ,  fmt(%9.0fc %9.2fc) labels(`"N"' `"\(F\)"'))
			/* Only when exporting to tex*/
		`tex_settigns'
		;
				

	*figures showing year indicators;
	reg Du_lane_miles_IH c.exp_L_IH_SF12a_r#ibn.year `keep_rule', noc  cluster(state);
	plot_dummies;
	erase "temp_t.pdf";
	copy "temp_betas.dta" "../intermediate_data/construction_betas_1.dta", replace ;
	erase "temp_betas.dta";	
	*The figure in the paper is created in the "optimality conditions dofile";

eststo dir ;

log close;
exit;
